package com.example.controller;

import cn.dev33.satoken.annotation.SaCheckDisable;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.example.model.DTO.InsuranceAccountDTO;
import com.example.model.DTO.MaskedUserDTO;
import com.example.model.DTO.UpdatePhoneDTO;
import com.example.model.DTO.UserDTO;
import com.example.model.Result;
import com.example.model.pojo.CaculateRetirement;
import com.example.model.pojo.InsuranceAccount;
import com.example.model.pojo.Person;
import com.example.service.AiService;
import com.example.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * 用户相关接口控制器
 * 提供用户注册、登录、养老金计算等功能
 *
 * @author zhexueqi
 * @since 2024/4/12
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户相关接口")
@RequiredArgsConstructor
@Validated
public class UserController {

    @ApiOperation(value = "封禁用户", notes = "管理员封禁用户账户")
    @PostMapping("/ban")
    @SaCheckRole("admin")
    public Result<String> banUser(@RequestParam Long userId,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime expiryTime,
            @RequestParam(required = true) Integer banType) {
        return userService.banUser(userId, expiryTime, banType);
    }

    @ApiOperation(value = "解封用户", notes = "管理员解除用户封禁")
    @PostMapping("/unban")
    @SaCheckRole("admin")
    public Result<String> unbanUser(@RequestParam Long userId, @RequestParam(required = false) Integer banType) {
        return userService.unbanUser(userId, banType);
    }

    private final UserService userService;

    private final AiService ai;

    @ApiOperation(value = "登录", notes = "登录")
    @PostMapping(value = "login")
    @CrossOrigin(originPatterns = "*")
    public Result<Object> login(@RequestBody UserDTO userDTO, HttpServletRequest request) {
        return userService.login(userDTO, request);
    }

    /**
     * 注销账号
     *
     * @return
     */
    @ApiOperation(value = "注销账号", notes = "注销账号")
    @GetMapping(value = "logout")
    public void logout() {
        log.info("接收到用户注销请求: userId: {}", StpUtil.getLoginId());
        StpUtil.logout();
    }

    @ApiOperation(value = "注册用户", notes = "创建新用户并生成区块链账户，返回账户地址和签名信息")
    @CrossOrigin
    @RequestMapping(value = "addPerson", method = RequestMethod.POST)
    public Result<String> addPerson(
            @ApiParam(value = "用户信息", required = true) @RequestBody Person person) {
        log.info("接收到用户注册请求: {}", person.getName());
        return userService.addPerson(person);
    }

    @ApiOperation(value = "注册员工", notes = "将已注册用户添加为企业员工，建立社保关系")
    @CrossOrigin
    @RequestMapping(value = "addEmployee", method = RequestMethod.POST)
    public Result<String> addEmployee(
            @ApiParam(value = "员工社保账户信息", required = true) @RequestBody InsuranceAccountDTO insuranceAccountDTO) {
        log.info("接收到员工注册请求，地址: {}", insuranceAccountDTO.getAddress());
        return userService.addEmployee(insuranceAccountDTO);
    }

    @ApiOperation(value = "缴纳养老保险", notes = "缴纳养老保险")
    @RequestMapping(value = "makePayment", method = RequestMethod.POST)
    @SaCheckDisable("money")
    public Result<String> makePayment(@RequestBody InsuranceAccount InsuranceAccount) {
        Object loginId = StpUtil.getLoginId();
        // 检查当前用户是否禁用资金操作
        StpUtil.checkDisable(loginId, "money");
        return userService.makePayment(InsuranceAccount);
    }

    @ApiOperation(value = "计算退休金", notes = "计算退休金")
    @PostMapping("/calculateRetirement")
    public Result<Double> calculateRetirement(@RequestBody CaculateRetirement calculateRetirementBo) {
        return userService.calculateRetirementPension(calculateRetirementBo);
    }

    @ApiOperation(value = "获取当前登录用户脱敏信息", notes = "返回当前登录用户的脱敏数据，包括身份证号、联系电话、电子邮箱和钱包地址")
    @GetMapping("/getCurrentUserInfo")
    @CrossOrigin
    public Result<MaskedUserDTO> getCurrentUserInfo() {
        return userService.getCurrentUserMaskedInfo();
    }

    /**
     * 更换头像
     */
    @ApiOperation(value = "更换头像", notes = "更换头像")
    @PostMapping("/updateAvatar")
    @CrossOrigin
    public Result<String> updateAvatar(@RequestParam("file") MultipartFile file) {
        log.info("接收到用户头像更新请求: userId: {}", StpUtil.getLoginId());
        if (file.isEmpty()) {
            return Result.error("上传的头像文件不能为空");
        }
        return userService.updateAvatar(file);
    }

    /**
     * 修改手机号
     */
    @ApiOperation(value = "验证原手机号", notes = "验证用户输入的原手机号是否与数据库一致")
    @PostMapping("/verifyOldPhone")
    @CrossOrigin
    public Result<String> verifyOldPhone(@RequestParam String oldPhone) {
        log.info("接收到原手机号验证请求: userId: {}", StpUtil.getLoginId());
        return userService.verifyOldPhone(oldPhone);
    }

    @ApiOperation(value = "更新手机号", notes = "更新当前登录用户的新手机号")
    @PostMapping("/updatePhone")
    @CrossOrigin
    public Result<String> updatePhone(@RequestParam String newPhone) {
        log.info("接收到手机号更新请求: userId: {}", StpUtil.getLoginId());
        return userService.updatePhone(newPhone);
    }
}
